<?php
/**
 * Created by PhpStorm.
 * User: DD
 * Date: 2016/4/18
 * Time: 16:43
 */

namespace app\admin\controller;


use app\admin\services\MenuService;
use think\Input;
use app\common\controller\Base;
class Menu extends Base
{
    /**
     * @var MenuService
     */
    protected $_service;

    protected function _setService()
    {
        $this->_service = new MenuService();
    }

    public function index()
    {
        return $this->fetch('menu/index');
    }

    public function getRoleMenusByPId()
    {
        return $this->returnJson($this->_service->getRoleMenusByPId(Input::get('pid')));
    }

    public function getMenusByPId()
    {
        $pid = I('get.pid', 0, 'intval');
        $root = I('get.root', 0, 'intval');
        $rt = $this->_service->getMenusByParentId($pid);
        if ($root) {
            array_unshift($rt, array(
                'id' => 0,
                'text' => 'Root'
            ));
        }
        return $this->returnJson($rt);
    }

    public function getMenusByRoleId()
    {
        $rid = I('get.rid', 0, 'intval');
        $rt = $this->_service->getMenusByRoleId($rid);
        return $this->returnJson($rt);
    }
}